<template>
	<view class="home">
		<!-- 轮播图 -->
		<swiper indicator-dots circular>
			<block v-for="item in swipers" :key="item.id">
				<swiper-item>
					<image class="swiper-image" :src="item.img"/>
				</swiper-item>
			</block>
		</swiper>
		<!-- 导航区域 -->
		<view class="nav">
			<block v-for="item in navs" :key="item.title">
				<view class="nav-item" @click="navItemClick(item.path)">
					<view :class="'iconfont icon-' + item.icon"></view>
					<text>{{item.title}}</text>
				</view>
			</block>
		</view>
		<!-- 推荐商品 -->
		<view class="hot-goods">
			<view class="title">
				推荐商品
			</view>
			<!-- 商品列表 -->
			<goods-list :goods="goods" @goodsClick="toDetail"></goods-list>
		</view>
	</view>
</template>

<script>
	import {getSwiper, getHotGoods} from '../../api/index/index.js';
	
	import GoodsList from '@/components/goods-list/goods-list.vue';
	
	export default {
		data() {
			return {
				swipers: [],
				goods: [],
				navs: [{
					icon: 'ziyuan',
					title: '黑马超市',
					path: '/pages/index/goods/goods'
				}, {
					icon: 'guanyuwomen',
					title: '联系我们',
					path: '/pages/index/contact/contact'
				}, {
					icon: 'tupian',
					title: '社区图片',
					path: '/pages/index/pics/pics'
				}, {
					icon: 'shipin',
					title: '学习视频',
					path: '/pages/index/videos/videos'
				}]
			}
		},
		onLoad() {
			// 获取轮播图数据
			this.getSwipers();
			// 获取热门商品
			this.getHotGoods();
		},
		methods: {
			/**
			 * 跳转到商品详情页
			 */
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/index/goods/detail?id=' + id 
				})
			},
			/**
			 * 获取轮播图数据
			 */
			getSwipers() {
				getSwiper().then(res => {
					this.swipers = res.data.message; 
				})
			},
			/**
			 * 获取热门商品
			 */
			getHotGoods() {
				getHotGoods().then(res => {
					this.goods = res.data.message;
				})
			},
			/**
			 * 跳转到指定路径
			 * @param {Object} path 指定的跳转路径
			 */
			navItemClick(path) {
				uni.navigateTo({
					url: path
				})
			}
		},
		components: {
			GoodsList
		}
	}
</script>

<style lang="scss" scoped>
	@import "~@/static/css/index/index.scss";
</style>
